﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace RESP_IIWaterAssessment
{
    public partial class ShowCanalsForm : Form
    {
        public ShowCanalsForm()
        {
            InitializeComponent();
        }

        int regID, avpID;
        tb_canal canal;

        private void cbRegion_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbRegion.SelectedIndex >= 0)
            {
                cbAVP.Enabled = true;
                regID = Convert.ToInt32(cbRegion.SelectedValue);
                cbAVP.SelectedIndexChanged -= cbAVP_SelectedIndexChanged;
                cbAVP.DataSource = UtilClass.FillAVP(regID);
                cbAVP.DisplayMember = "Name";
                cbAVP.ValueMember = "ID";
                cbAVP.SelectedIndex = -1;
                cbAVP.Text = string.Empty;
                cbAVP.SelectedIndexChanged += new EventHandler(cbAVP_SelectedIndexChanged);
            }
            else
            {
                cbAVP.Enabled = false;
                regID = 0;
            }
        }

        private void cbAVP_SelectedIndexChanged(object sender, EventArgs e)
        {
            avpID = (cbAVP.SelectedIndex >= 0) ? Convert.ToInt32(cbAVP.SelectedValue) : 0;
        }

        private void ShowCanalsForm_Load(object sender, EventArgs e)
        {
            cbAVP.Enabled = false;

            cbRegion.SelectedIndexChanged -= cbRegion_SelectedIndexChanged;
            cbRegion.DataSource = UtilClass.FillCBRegions();
            cbRegion.DisplayMember = "Name";
            cbRegion.ValueMember = "ID";
            cbRegion.SelectedIndex = -1;
            cbRegion.Text = string.Empty;
            cbRegion.SelectedIndexChanged += new EventHandler(cbRegion_SelectedIndexChanged);
        }

        private void buttonShow_Click(object sender, EventArgs e)
        {
            ShowCanals();
        }

        private void ShowCanals()
        {
            if (avpID > 0)
            {
                dgvCanals.DataSource = DBClass.Canals
                    .Where(wh => wh.AVPID == avpID)
                    .ToList();
                GridSettings();
            }
        }

        private void GridSettings()
        {
            if (dgvCanals.ColumnCount > 0)
            {
                dgvCanals.Columns["ID"].Visible = dgvCanals.Columns["AVPID"].Visible =
                    dgvCanals.Columns["tb_avp"].Visible = dgvCanals.Columns["CoverType"].Visible = false;

                if (InformationClass.Lang)
                {
                    dgvCanals.Columns["Name"].HeaderText = "Название";
                    dgvCanals.Columns["Area"].HeaderText = "Площадь (га)";
                    dgvCanals.Columns["WaterUser"].HeaderText = "Водопользователей";
                    dgvCanals.Columns["Length"].HeaderText = "Протяженность (км)";
                    dgvCanals.Columns["Note"].HeaderText = "Примечание";
                }
                else
                {
                    dgvCanals.Columns["Name"].HeaderText = "Номи";
                    dgvCanals.Columns["Area"].HeaderText = "Майдон (га)";
                    dgvCanals.Columns["WaterUser"].HeaderText = "Сувдан фойдаланувчилиар";
                    dgvCanals.Columns["Length"].HeaderText = "Масофа (км)";
                    dgvCanals.Columns["Note"].HeaderText = "Изоҳлар";
                }

                dgvCanals.AutoResizeColumns();
            }
        }

        private void buttonOK_Click(object sender, EventArgs e)
        {
            DBClass.RESPContext.SubmitChanges();
            this.Close();
        }

        private void tsbAdd_Click(object sender, EventArgs e)
        {
            if (avpID > 0)
            {
                EditCanalForm canalForm = new EditCanalForm(null, avpID);
                if (canalForm.ShowDialog() == DialogResult.OK)
                {
                    if (canalForm.Canal != null)
                    {
                        try
                        {
                            DBClass.Canals.InsertOnSubmit(canalForm.Canal);
                            DBClass.RESPContext.SubmitChanges();
                            // добавить добавление состояния и пр поебень
                            ShowCanals();
                            MessageBox.Show(msgClass.msgSubmitChanges, msgClass.msgAchtung);
                        }
                        catch
                        {
                            MessageBox.Show(msgClass.msgError, msgClass.msgAchtung);
                        }
                    }
                }
            }
        }

        private void tsbEdit_Click(object sender, EventArgs e)
        {
            if (dgvCanals.RowCount > 0 && dgvCanals.CurrentRow != null)
            {
                canal = DBClass.Canals.Where(wh => wh.ID == (int)dgvCanals["ID", dgvCanals.CurrentRow.Index].Value).SingleOrDefault();
                EditCanalForm canalForm = new EditCanalForm(canal, canal.AVPID);
                if (canalForm.ShowDialog() == DialogResult.OK)
                {
                    canal = canalForm.Canal;
                    if (DBClass.RESPContext.GetChangeSet().Deletes.Count > 0 || DBClass.RESPContext.GetChangeSet().Inserts.Count > 0 || DBClass.RESPContext.GetChangeSet().Updates.Count > 0)
                    {
                        DBClass.RESPContext.SubmitChanges();
                    }
                    ShowCanals();
                }
            }
        }

        private void tsbDelete_Click(object sender, EventArgs e)
        {
            if (dgvCanals.RowCount > 0 && dgvCanals.CurrentRow != null)
            {
                string msg = InformationClass.Lang ? "Удалить канал?" : "Канал олиб ташлансинми?";

                if (MessageBox.Show(msg, msgClass.msgAchtung, MessageBoxButtons.OKCancel) == DialogResult.OK)
                {
                    canal = DBClass.Canals.Where(wh => wh.ID == (int)dgvCanals["ID", dgvCanals.CurrentRow.Index].Value).SingleOrDefault();
                    DBClass.Canals.DeleteOnSubmit(canal);
                    DBClass.RESPContext.SubmitChanges();
                    ShowCanals();
                }
            }
        }
    }
}
